package com.waimai.mobile.ui.activity;

import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.waimai.mobile.R;
import com.waimai.mobile.api.WaiMaiServer;
import com.waimai.mobile.api.entity.User;
import com.waimai.mobile.api.model.ResponseModel;
import com.waimai.mobile.ui.WaiMaiApp;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

/**
 * Created by lin on 2015/6/15.
 */
@EActivity(R.layout.activity_login)
public class LoginActivity extends BaseActivity02{

    public final static int REQUEST_LOGIN = 10;
    public final static int RESULT_LOGIN_SUCCESS = 11;
    public final static int RESULT_LOGIN_FAILED = 12;

    @App
    public WaiMaiApp app;

    @RestService
    public WaiMaiServer server;

    @ViewById(R.id.userNameOrPhoneEdt)
    public EditText userNameOrPhoneEdt;

    @ViewById(R.id.passwordEdt)
    public EditText passwordEdt;

    @ViewById(R.id.loginBtn)
    public Button loginBtn;

    @ViewById(R.id.toRegistBtn)
    public TextView toRegistBtn;

    @AfterInject
    public void afterInject(){
        server.setHeader("Content-Type", "application/json");
    }

    @AfterViews
    public void afterViews(){
        setActionBarTitle("登录");
        Toast.makeText(this, "请登录", Toast.LENGTH_SHORT).show();
    }

    @Click(R.id.loginBtn)
    public void loginBtnClick(){
        String userNameOrPhone = userNameOrPhoneEdt.getText().toString().trim();
        String password = passwordEdt.getText().toString().trim();

        if(userNameOrPhone == null || "".equals(userNameOrPhone)){
            Toast.makeText(getApplicationContext(), "请输入用户名或手机号", Toast.LENGTH_SHORT).show();
            return ;
        }
        if(password == null || "".equals(password)){
            Toast.makeText(getApplicationContext(), "请输入密码", Toast.LENGTH_SHORT).show();
            return ;
        }

        submitLoginRequest(new User(userNameOrPhone, userNameOrPhone, password));
    }

    @Click(R.id.toRegistBtn)
    public void toRegistBtnClick(){
        Intent intent = new Intent(getApplicationContext(), RegistActivity_.class);
        startActivity(intent);
    }

    @Background
    public void submitLoginRequest(User user){
        try {
            ResponseModel<User> model = server.login(user);
            if(model != null){
                switch (model.getStatus().getStatusCode()) {
                    case ResponseModel.STATUS_LOGIN_SUCCESS:
                        loginSuccess(model.getData());
                        break;
                    case ResponseModel.STATUS_LOGIN_FAILED:
                        loginFailed();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                        break;
                }
            }
        }catch (Exception e){
            netError();
        }
    }

    @UiThread
    public void loginSuccess(User user){
        app.setUser(user);
        Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
        setResult(RESULT_LOGIN_SUCCESS);
        finish();
    }

    @UiThread
    public void loginFailed(){
        Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
        setResult(RESULT_LOGIN_FAILED);
    }

    @UiThread
    public void serverError(){
        Toast.makeText(getApplicationContext(), "服务器异常，请重试", Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void netError(){
        Toast.makeText(getApplicationContext(), "网络错误，请重试", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(app.getUser() != null){
            onBackPressed();
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_LOGIN_FAILED);
        super.onBackPressed();
    }
}
